<template>
  <div
    style="
      width: 1015px;
      height: 105px;
      margin-left: 200px;
      top: 25px;
      background-color: rgb(255, 255, 255);
      position: absolute;
    "
  >
    <el-form ref="queryFormRef" :model="QueryData.form">
      <el-form-item prop="courseSql" class="inputSql">
        <el-input
          v-model="QueryData.form.courseSql"
          type="search"
          placeholder="按课程名查询"
        />
      </el-form-item>
      <el-form-item prop="teacherSql" class="inputSql">
        <el-input
          v-model="QueryData.form.teacherSql"
          type="search"
          placeholder="按教师查询"
        />
      </el-form-item>

      <el-button type="primary" class="querybt1" @click="req(queryFormRef)"
        >查询☚</el-button
      >
      <el-button type="primary" class="querybt1" @click="reload()"
        >刷新☚</el-button
      >
    </el-form>

    <div class="container">
      <ul class="tableheader">
        <li style="color: Navy">课程名</li>
        <li style="color: Navy">上课地点</li>
        <li style="color: Navy">任课教师</li>
        <li style="color: Navy">详细信息</li>
      </ul>
      <br />
      <hr />
      <ul v-if="dataRef.length" class="tablebody">
        <li v-for:="(item, key) in dataRef">
          <div class="row" style="padding-left: 70px">
            {{ item.name }}
          </div>
          <div class="row">{{ item.address }}</div>
          <div class="row">{{ item.teacher }}</div>
          <div class="row">~~~</div>
          <el-button type="primary" class="querybt" @click="drawBox(item.id)"
            >修改</el-button
          >
          <el-button type="primary" class="querybt" @click="deleteReq(item.id)"
            >删除</el-button
          >
        </li>
      </ul>
      <p v-else>
        <img
          src="https://img-qn-0.51miz.com/preview/element/00/01/13/33/E-1133330-94144800.jpg!/quality/90/unsharp/true/compress/true/format/jpg/fh/260"
        />
      </p>
    </div>
    <div class="add">
      <hr />
      <h2
        style="
          color: Navy;
          font-size: 20px;
          font-weight: 900px;
          position: absolute;
          top: 0;
        "
      >
        添加课程信息
      </h2>
      <el-form ref="insertFormRef" :model="insertData.form">
        <el-form-item prop="ename">
          <el-input
            v-model="insertData.form.name"
            type="text"
            class="insertinput_name"
            placeholder="课程名"
          />
        </el-form-item>

        <el-form-item prop="teacher">
          <el-input
            v-model="insertData.form.teacher"
            type="text"
            class="insertinput_teacher"
            placeholder="任课教师"
          />
        </el-form-item>

        <el-form-item prop="address">
          <el-input
            v-model="insertData.form.address"
            type="text"
            class="insertinput_room"
            placeholder="上课地点"
          />
        </el-form-item>
        <el-button
          type="primary"
          class="insertbt"
          @click="insertReq(insertFormRef)"
          >添加</el-button
        >
      </el-form>
    </div>
    <!-- 更新框 -->
    <div class="updatebox">
      <h2
        style="
          color: Navy;
          font-size: 20px;
          font-weight: 900px;
          position: absolute;
          top: 0;
        "
      >
        修改课程信息
      </h2>
      <el-form ref="updateFormRef" :model="updateData.form">
        <el-form-item prop="teacher">
          <el-input
            v-model="updateData.form.teacher"
            type="text"
            class="updateinput"
            placeholder="任课教师"
          />
        </el-form-item>
        <el-form-item prop="name">
          <el-input
            v-model="updateData.form.name"
            type="text"
            class="updateinput"
            style="margin-top: 20px"
            placeholder="课程名"
          />
        </el-form-item>
        <el-form-item prop="length">
          <el-input
            v-model="updateData.form.length"
            type="text"
            class="updateinput"
            style="margin-top: 40px"
            placeholder="课时"
          />
        </el-form-item>
        <el-form-item prop="week">
          <el-input
            v-model="updateData.form.week"
            type="text"
            class="updateinput"
            style="margin-top: 60px"
            placeholder="课程周期(例:1~16)"
          />
        </el-form-item>
        <el-form-item prop="address">
          <el-input
            v-model="updateData.form.address"
            type="text"
            class="updateinput"
            style="margin-top: 80px"
            placeholder="上课地点(例:博学楼101)"
          />
        </el-form-item>
        <el-button
          type="primary"
          class="updatebt"
          @click="updateReq(updateFormRef)"
          >确认修改</el-button
        >
        <el-button
          type="primary"
          class="updatebt"
          style="left: 650px"
          @click="cleanBox()"
          >取消</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "@vue/reactivity";
import { query_Course } from "../../apis/query/queryCourse.js";
import { delete_Course } from "../../apis/delete/deleteCourse.js";
import { insert_Course } from "../../apis/inset/insertCourse.js";
import { update_Course } from "../../apis/update/updateCourse.js";
const queryFormRef = ref();
let dataRef = reactive([]);
//查询
const QueryData = reactive({
  form: {
    courseSql: "",
    tescherSql: "",
  },
  rules: {},
});
const req = async (queryFormRef) => {
  await queryFormRef.validate((valid) => {
    //校验表单是否通过
    if (valid) {
      query_Course(QueryData.form).then((response) => {
        var x;
        for (x in response.data) {
          dataRef[x] = response.data[x];
          console.log(dataRef[x]);
        }
      });
    }
  });
};
//删除
const deletedata = reactive({ form: { id: 0 }, rules: {} });
const deleteReq = async (id) => {
  deletedata.form.id = id;
  delete_Course(deletedata.form).then((res) => {
    console.log(res?.message);
  });
};
//插入数据
const insertFormRef = ref();
const insertData = reactive({
  form: {
    name: "",
    teacher: "",
    address: "",
  },
});
const insertReq = async (insertFormRef) => {
  await insertFormRef.validate((valid) => {
    //校验表单是否通过
    if (valid) {
      insert_Course(insertData.form).then((response) => {
        console.log(response?.message);
      });
    }
  });
};
//更新数据
const updateFormRef = ref();
const updateData = reactive({
  form: {
    form: {
      teacher: "",
      name: "",
      address: "",
      week: "",
      length: null,
    },
  },
});
const updateReq = async (updateFormRef) => {
  await updateFormRef.validate((valid) => {
    //校验表单是否通过
    if (valid) {
      update_Course(updateData.form).then((response) => {
        console.log(response?.message);
      });
    }
  });
};
const drawBox = (id) => {
  $(".updatebox").css("z-index", "999");
  updateData.form.id = id;
};
const cleanBox = () => {
  $(".updatebox").css("z-index", "-1");
};
const reload = () => {
  console.log("刷新！");
  location.reload();
};
</script>

<style scoped>
.inputSql {
  width: 150px;
  padding: 50px 30px;
  float: left;
}
.querybt {
  margin: 24px;
  float: left;
}
.querybt1 {
  margin: 50px;
  float: left;
}
.container {
  position: absolute;
  width: 1015px;
  height: 455px;
  margin-top: 105px;
  background-color: white;
}
.tableheader li {
  width: 150px;
  padding: 0px 19px;
  float: left;
  font-size: 25px;
  font-weight: 900;
  text-align: left;
}
.tableheader {
  list-style-type: none;
}
.tablebody {
  width: calc(100%);
  max-height: 400px;
  overflow-y: scroll;
  overflow-x: hidden;
  list-style-type: none;
  float: left;
  text-align: left;
  position: absolute;
  top: 38px;
  left: -40px;
}
li {
  display: flex;
  width: 1015px;
}
li div {
  width: 305px;
  height: 30px;
}
.tablebody li:hover {
  background-color: rgb(230, 245, 245);
}
.add {
  position: absolute;
  top: 550px;
  height: 150px;
  width: 1015px;
  background-color: white;
  float: left;
}
.insertinput_name {
  position: absolute;
  width: 150px;
  top: 30px;
  left: 50px;
}
.insertbt {
  position: absolute;
  width: 100px;
  height: 40px;
  left: 800px;
  top: 50px;
}
.insertinput_teacher {
  position: absolute;
  width: 150px;
  top: 12px;
  left: 250px;
}
.insertinput_room {
  position: absolute;
  width: 150px;
  top: -6px;
  left: 450px;
}
.updatebox {
  position: absolute;
  top: 100px;
  height: 450px;
  width: 1015px;
  background-color: white;
  float: left;
  z-index: -1;
}
.updateinput {
  position: absolute;
  width: 200px;
  max-height: 230px;
  overflow: hidden;
  top: 30px;
  left: 350px;
}
.updatebt {
  position: absolute;
  width: 100px;
  height: 40px;
  left: 150px;
  top: 300px;
}
.row {
  overflow: hidden;
  height: 50px;
}
</style>
